﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LikeUs.WebService.Client;
using LikeUs.WebService.Client.WSInfoClient;
using System.Web.Security;
using System.Data;
using LikeUs.MovieServices;

namespace LikeUsWebSite
{
    public partial class Friend : System.Web.UI.Page
    {
        protected UserData userData;
        protected void Page_Load(object sender, EventArgs e)
        {
            getUserInfo();
            getMyRatings();
            getUserLogs();
        }

        protected void getUserInfo()
        {
            int userid=Int32.Parse(Request.QueryString["id"].ToString());
            WSInfoProxy wsProxy = new WSInfoProxy();
            userData = (UserData)wsProxy.GetUser(userid, Session["AccessToken"]);
            userName.Text = userData.Name1;
            userMail.Text = userData.Email;
            myImage.ImageUrl = userData.Image;
            if (myImage.ImageUrl=="")
                myImage.ImageUrl = "./Images/likeus2.jpg";
            if (userData.AccountType!="Facebook")
                typeImg.ImageUrl = "./Images/likeus2.jpg";
            else
                typeImg.ImageUrl = "./Images/Facebook.png";
        }


        protected void getMyRatings()
        {
            DataTable dt;
            WSInfoProxy wsProxy = new WSInfoProxy();
            int userid = -1;
            if (Request.QueryString["id"] != null)
                userid = Int32.Parse(Request.QueryString["id"].ToString());
            //UserData user = wsProxy.GetMyUser(Page.User.Identity.Name);
            UserData user = (UserData)Session["userData"];
            dt = wsProxy.friendRatings(userid, user.UserID);
            MovieApiService likeusApi = new MovieApiService();
            dt.Columns.Add("name");
            dt.Columns.Add("image");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                LikeUsMovie movie = likeusApi.GetMovieById((int)dt.Rows[i]["idmovies"]);
                dt.Rows[i]["name"] = movie.Title;
                dt.Rows[i]["image"] = movie.PosterSmall;
            }
            DataGrid1.DataSource = dt;
            DataGrid1.DataBind();

        }

        protected void getUserLogs()
        {
            string[] dt;
            WSInfoProxy wsProxy = new WSInfoProxy();
            int userid = -1;
            if (Request.QueryString["id"] != null)
            {
                userid = Int32.Parse(Request.QueryString["id"].ToString());
                dt = wsProxy.getLog(userid, 10);
                if (dt == null)
                {
                    TabPanel1.Controls.Add(new LiteralControl("<h3>Nothing To Tell About...</h3>"));
                    return;
                }
                TabPanel1.Controls.Add(new LiteralControl("<table cellspacing='5'>"));
                for (int i = 0; i < dt.Length;i+=2 )
                    TabPanel1.Controls.Add(new LiteralControl("<tr><td><div style='background-color:#EDEFF4;border:1px solid #D8DFEA;'>" + 
                        "<table cellpadding='5'><tr><td><img src='"+userData.Image+"' height='50'/></td><td>"+dt[i] +"</td></tr><tr><td><i><b><font size='1'>"+ dt[i+1]+
                        "</font></b></i></td></tr></table></div></td></tr>"));
                TabPanel1.Controls.Add(new LiteralControl("</table>"));
            }
        }

        protected void Button5_Click(object sender, EventArgs e)
        {
            /*WSInfoProxy wsProxy = new WSInfoProxy();
            wsProxy.DeleteUserAccount(userData.UserID);
            Session.Abandon();
            Session.Clear();
            FormsAuthentication.SignOut();
            Response.Redirect("./Default.aspx");*/
        }
    }
}
